<template>
  <t-head-menu :value="activeMenu" @change="handleSelect" v-if="splitMenu">
    <t-menu-item
      :style="{ '--theme': theme }"
      :value="item.path"
      v-for="(item, index) in topMenus"
      :key="index"
    >
      <template #icon>
        <svg-icon :icon-class="item.meta.icon" />
      </template>

      <template #content>
        {{ item.meta.title }}
      </template>
    </t-menu-item>
  </t-head-menu>
  <!--TODO expand-type="popup" 时存在bug，第一次无法选中 -->
  <t-head-menu
    v-else
    :value="activeMenu"
    @change="handleSelect"
    expand-type="popup"
    :theme="'light'"
  >
    <TopNavItem
      v-for="(route, index) in topMenus"
      :key="index"
      :item="route"
    ></TopNavItem>
  </t-head-menu>
</template>

<script setup>
import { constantRoutes } from '@/router';
import { isHttp } from '@/utils/validate';
import TopNavItem from './TopNavItem.vue';
import useAppStore from '@/store/modules/app';
import useSettingsStore from '@/store/modules/settings';
import usePermissionStore from '@/store/modules/permission';
import variables from '@/assets/styles/variables.module.scss';

// 顶部栏初始数
const visibleNumber = ref(null);
// 当前激活菜单的 index
const currentIndex = ref(null);
// 隐藏侧边栏路由
const hideList = ['/index', '/user/profile'];

const appStore = useAppStore();
const settingsStore = useSettingsStore();
const permissionStore = usePermissionStore();
const route = useRoute();
const router = useRouter();

// 主题颜色
const theme = computed(() => settingsStore.theme);
// 拆分菜单
const splitMenu = computed(() => settingsStore.splitMenu);
// 所有的路由信息
const routers = computed(() => permissionStore.topbarRouters);

// 顶部显示菜单
const topMenus = computed(() => {
  let topMenus = [];
  routers.value.map((menu) => {
    if (menu.hidden !== true) {
      // 兼容顶部栏一级菜单内部跳转
      if (menu.path === '/') {
        topMenus.push(menu.children[0]);
      } else {
        topMenus.push(menu);
      }
    }
  });
  // console.log('topMenus', topMenus);
  // console.log('spilMenu', splitMenu.value);
  return topMenus;
});

// 默认激活的菜单
const activeMenu = computed(() => {
  const path = route.path;
  let activePath = path;
  if (splitMenu.value) {
    if (
      path !== undefined &&
      path.lastIndexOf('/') > 0 &&
      hideList.indexOf(path) === -1
    ) {
      const tmpPath = path.substring(1, path.length);
      activePath = '/' + tmpPath.substring(0, tmpPath.indexOf('/'));
      if (!route.meta.link) {
        appStore.toggleSideBarHide(false);
      }
    } else if (!route.children) {
      activePath = path;
      appStore.toggleSideBarHide(true);
    }
  }
  activeRoutes(activePath);
  return activePath;
});

// 设置子路由
const childrenMenus = computed(() => {
  // console.log('route', route);
  let childrenMenus = [];
  routers.value.map((router) => {
    for (let item in router.children) {
      if (router.children[item].parentPath === undefined) {
        if (router.path === '/') {
          router.children[item].path = '/' + router.children[item].path;
        } else {
          if (!isHttp(router.children[item].path)) {
            router.children[item].path =
              router.path + '/' + router.children[item].path;
          }
        }
        router.children[item].parentPath = router.path;
      }
      childrenMenus.push(router.children[item]);
    }
  });
  return constantRoutes.concat(childrenMenus);
});

function handleSelect(key) {
  // console.log('handleSelect', key);
  currentIndex.value = key;
  const route = routers.value.find((item) => item.path === key);
  if (isHttp(key)) {
    // http(s):// 路径新窗口打开
    window.open(key, '_blank');
  } else if (!route || !route.children) {
    // 没有子路由路径内部打开
    router.push({ path: key });
    appStore.toggleSideBarHide(true);
  } else {
    if (splitMenu) {
      // 显示左侧联动菜单
      activeRoutes(key);
      appStore.toggleSideBarHide(false);
    }
  }
}

function activeRoutes(key) {
  let routes = [];
  if (childrenMenus.value && childrenMenus.value.length > 0) {
    childrenMenus.value.map((item) => {
      if (key == item.parentPath || (key == 'index' && '' == item.path)) {
        routes.push(item);
      }
    });
  }
  if (routes.length > 0) {
    permissionStore.setSidebarRouters(routes);
  } else {
    appStore.toggleSideBarHide(true);
  }
  return routes;
}
</script>

<style lang="scss">
.topmenu-container.t-menu--horizontal > .t-menu-item {
  float: left;
  height: 50px !important;
  line-height: 50px !important;
  color: #999093 !important;
  padding: 0 5px !important;
  margin: 0 10px !important;
}

.topmenu-container > .is-active {
  border-bottom: 2px solid #{'var(--theme)'} !important;
  color: #303133;
}

/* sub-menu item */
.topmenu-container.t-menu--horizontal > .t-sub-menu .t-sub-menu__title {
  float: left;
  height: 50px !important;
  line-height: 50px !important;
  color: #999093 !important;
  padding: 0 5px !important;
  margin: 0 10px !important;
}
</style>
